package sing.myapp.ui.calc

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.Spinner
import android.widget.TabHost
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import sing.core.base.BaseActivity
import sing.core.dialog.BottomSheetListDialog
import sing.core.ext.util.showShort
import sing.core.ext.view.clickNoRepeat
import sing.core.ext.view.isStatusBarBg
import sing.core.network.bean.IdNameValueBean
import sing.myapp.BR
import sing.myapp.R
import sing.myapp.databinding.ActivityCalcBinding
import sing.myapp.router.RouterURL
import java.math.BigDecimal

/**
 * 计算器
 */
@Route(path = RouterURL.PAGE_CALC)
class CalcActivity : BaseActivity<CalcViewModel, ActivityCalcBinding>(R.layout.activity_calc,BR.calcVm) {

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        db.status.isStatusBarBg()
        db.ivBack.clickNoRepeat { onBackPressed() }
    }

    override fun initData() {
        super.initData()
        db.llChooseYear.clickNoRepeat { chooseYear() }
        db.tvSubmit.clickNoRepeat {
            when(vm.pos.value){
                0,1 -> submit12()
                2 -> submit3()
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        vm.pos.observe(this){
            vm.reSet()
            when(it){
                0 -> db.etRate.setText("6.55")
                1 -> db.etRate.setText("4.50")
                2 -> {
                    db.etRate1.setText("6.55")
                    db.etRate2.setText("4.50")
                }
            }
        }
    }

    // 选择按揭年数
    fun chooseYear(){
        val list = arrayListOf<IdNameValueBean>()
        list.add(IdNameValueBean("1","1年"))
        list.add(IdNameValueBean("2","2年"))
        list.add(IdNameValueBean("3","3年"))
        list.add(IdNameValueBean("4","4年"))
        list.add(IdNameValueBean("5","5年"))
        list.add(IdNameValueBean("10","10年"))
        list.add(IdNameValueBean("15","15年"))
        list.add(IdNameValueBean("20","20年"))
        list.add(IdNameValueBean("25","25年"))
        list.add(IdNameValueBean("30","30年"))
        BottomSheetListDialog(this,list,object : BottomSheetListDialog.OnClickListener{
            override fun result(data: IdNameValueBean) {
                vm.chooseYear.value = Integer.parseInt(data.id)
            }
        }).show()
    }

    fun submit12(){
        val value = "${db.etMoney.text.trim()}"
        if (value.isEmpty()) {
            "贷款总额不能为空".showShort()
            return
        }
        val value1 = "${db.etRate.text.trim()}"
        if (value1.isEmpty()) {
            "利率不能为空".showShort()
            return
        }
        cal(value.toDouble(), vm.chooseYear.value!! * 12.0, value1.toDouble() / 100)
    }

    fun submit3(){
        val money1 = "${db.etMoney2.text.trim()}"
        if (money1.isEmpty()) {
            "公积金贷款总额不能为空".showShort()
            return
        }

        val money2 = "${db.etMoney1.text.trim()}"
        if (money2.isEmpty()) {
            "商业贷款总额不能为空".showShort()
            return
        }

        val rate1 = "${db.etRate1.text.trim()}"
        if (rate1.isEmpty()) {
            "公积金利率不能为空".showShort()
            return
        }

        val rate2 = "${db.etRate2.text.trim()}"
        if (rate2.isEmpty()) {
            "商贷利率不能为空".showShort()
            return
        }

        cal(money1.toDouble(), vm.chooseYear.value!! * 12.0, rate1.toDouble() / 100, money2.toDouble(), rate2.toDouble() / 100)
    }

    fun cal(ze: Double, nx: Double, rate: Double) {
        val zem = (ze * rate / 12 * Math.pow((1 + rate / 12), nx)) / (Math.pow((1 + rate / 12), nx) - 1)
        val amount = zem * nx
        val rateAmount = amount - ze

        val zemvalue = BigDecimal(zem)
        val zemval = zemvalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        val amountvalue = BigDecimal(amount)
        val amountval = amountvalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        val rateAmountvalue = BigDecimal(rateAmount)
        val rateAmountval = rateAmountvalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        val benjinm = ze / nx
        val lixim = ze * (rate / 12)
        val diff = benjinm * (rate / 12)
        val huankuanm = benjinm + lixim
        val zuihoukuan = diff + benjinm
        val av = (huankuanm + zuihoukuan) / 2
        val zong = av * nx
        val zongli = zong - ze

        val huankuanmvalue = BigDecimal(huankuanm)
        val huankuanmval = huankuanmvalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        val diffvalue = BigDecimal(diff)
        val diffmval = diffvalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        val zongvalue = BigDecimal(zong)
        val zongval = zongvalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        val zonglivalue = BigDecimal(zongli)
        val zonglival = zonglivalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        vm.applyMoney.value = "$ze" // 贷款总额
        vm.repaymentMonth.value = "$nx" // 还款月数
        vm.everyMonth.value = "$zemval" // 等额本金 每月还款
        vm.firstMonth.value = "$huankuanmval" // 等额本金 第一个月还款
        vm.decrement.value = "$diffmval" // 等额本息 每月递减
        vm.totalMoney1.value = "$zongval" // 等额本金还款总额
        vm.totalMoney2.value = "$amountval" // 等额本息还款总额
        vm.interest1.value = "$zonglival" // 等额本金总利息
        vm.interest2.value = "$rateAmountval" // 等额本息总利息
    }


    fun cal(ze: Double, nx: Double, rate: Double, ze1: Double, rate1: Double) {
        val zem = (ze * rate / 12 * Math.pow((1 + rate / 12), nx)) / (Math.pow((1 + rate / 12), nx) - 1)
        val amount = zem * nx
        val rateAmount = amount - ze

        val zem1 = (ze1 * rate1 / 12 * Math.pow((1 + rate1 / 12), nx)) / (Math.pow((1 + rate1 / 12), nx) - 1);
        val amount1 = zem1 * nx
        val rateAmount1 = amount1 - ze1

        val zemvalue = BigDecimal(zem + zem1)
        val zemval = zemvalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        val amountvalue = BigDecimal(amount + amount1)
        val amountval = amountvalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        val rateAmountvalue = BigDecimal(rateAmount + rateAmount1)
        val rateAmountval = rateAmountvalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        val benjinm = ze / nx
        val lixim = ze * (rate / 12)
        val diff = benjinm * (rate / 12)
        val huankuanm = benjinm + lixim
        val zuihoukuan = diff + benjinm
        val av = (huankuanm + zuihoukuan) / 2
        val zong = av * nx
        val zongli = zong - ze

        val benjinm1 = ze1 / nx
        val lixim1 = ze1 * (rate1 / 12)
        val diff1 = benjinm1 * (rate1 / 12)
        val huankuanm1 = benjinm1 + lixim1
        val zuihoukuan1 = diff1 + benjinm1
        val av1 = (huankuanm1 + zuihoukuan1) / 2
        val zong1 = av1 * nx
        val zongli1 = zong1 - ze1

        val huankuanmvalue = BigDecimal(huankuanm + huankuanm1)
        val huankuanmval = huankuanmvalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        val diffvalue = BigDecimal(diff + diff1)
        val diffmval = diffvalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        val zongvalue = BigDecimal(zong + zong1)
        val zongval = zongvalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        val zonglivalue = BigDecimal(zongli + zongli1)
        val zonglival = zonglivalue.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()

        vm.applyMoney.value = "${ze + ze1}" // 贷款总额
        vm.repaymentMonth.value = "$nx" // 还款月数
        vm.everyMonth.value = "$zemval" // 等额本金 每月还款
        vm.firstMonth.value = "$huankuanmval" // 等额本金 第一个月还款
        vm.decrement.value = "$diffmval" // 等额本息 每月递减
        vm.totalMoney1.value = "$zongval" // 等额本金还款总额
        vm.totalMoney2.value = "$amountval" // 等额本息还款总额
        vm.interest1.value = "$zonglival" // 等额本金总利息
        vm.interest2.value = "$rateAmountval" // 等额本息总利息
    }
}